package logger

import (
	"fmt"
	"log"
	"os"
	"time"
)

// LogError 当存在错误时记录日志
func LogError(err error, prefix string) {
	if len(prefix) == 0 {
		prefix = "default"
	}
	file := "./data/logs/" + time.Now().Format("20180102") + ".log"
	logFile, err2 := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
	if err2 != nil {
		fmt.Printf("%+v", err2)
		panic(err2)
	}
	log.SetOutput(logFile) // 将文件设置为log输出的文件
	log.SetPrefix("["+prefix+"]")
	log.SetFlags(log.LstdFlags | log.Lshortfile | log.LUTC)

	//打印日志到log文件上
	if err != nil {
		log.Println(err)
	}
}

// LogWrite 当存在错误时记录日志
func LogWrite(err error, prefix string) {
	if len(prefix) == 0 {
		prefix = "default"
	}
	now  := time.Now()
	dateString := fmt.Sprintf("%d%d%d",now.Year(),now.Month(),now.Day())
	file := "./data/logs/" + dateString + ".log"
	logFile, err2 := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
	if err2 != nil {
		fmt.Printf("%+v", err2)
		panic(err2)
	}
	log.SetOutput(logFile) // 将文件设置为log输出的文件
	log.SetPrefix("["+prefix+"]")
	log.SetFlags(log.LstdFlags | log.Lshortfile | log.LUTC)

	//打印日志到log文件上
	if err != nil {
		log.Println(err)
	}
}
